package com.tpyule.tpclass.service.impl;

import com.tpyule.tpclass.dao.PathDao;
import com.tpyule.tpclass.entity.bo.MenuDetailBo;
import com.tpyule.tpclass.entity.bo.MenuPathBo;
import com.tpyule.tpclass.entity.bo.PathBo;
import com.tpyule.tpclass.entity.bo.PathDetailBo;
import com.tpyule.tpclass.entity.dto.MenuPathQueryDto;
import com.tpyule.tpclass.entity.dto.PathQueryDto;
import com.tpyule.tpclass.entity.dto.PathSelDto;
import com.tpyule.tpclass.entity.pojo.Path;
import com.tpyule.tpclass.entity.pojo.PathGroup;
import com.tpyule.tpclass.entity.pojo.Role;
import com.tpyule.tpclass.service.MenuService;
import com.tpyule.tpclass.service.PathService;
import com.tpyule.tpclass.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zzz
 * @version 1.0
 * @date 2020/4/24 21:01
 */
@Service
public class PathServiceImpl implements PathService {
    @Autowired
    PathDao pathDao;
    @Autowired
    RoleService roleService;
    @Autowired
    MenuService menuService;
    @Override
    public int getSizePath(PathQueryDto dto) {
        return pathDao.getSizePath(dto);
    }

    @Override
    public List<PathDetailBo> getListPath(PathQueryDto dto) {
        dto.setStart((dto.getPage() - 1) * dto.getPageSize());
        return pathDao.getListPath(dto);
    }

    @Override
    public int addPath(Path path) {
        return pathDao.addPath(path);
    }

    @Override
    public int updatePath(Path path) {
        return pathDao.updatePath(path);
    }

    @Override
    public int deletePath(Path path) {
        return pathDao.deletePath(path);
    }

    @Override
    public List<PathGroup> getListGroup() {
        return pathDao.getListGroup();
    }

    @Override
    public List<Path> getAllPath() {
        return pathDao.getAllPath();
    }

    @Override
    public List<PathBo> getMenu(PathSelDto dto) {
        dto.setLevel(1);
        List<PathBo> bos = pathDao.getMenu(dto);
        // 表示管理
        if (dto.getGroupId()==4){
            for (int i = 0; i < bos.size(); i++) {
                PathSelDto dtoo = new PathSelDto();
                dtoo.setGroupId(dto.getGroupId());
                dtoo.setParentId(bos.get(i).getId());
                List<PathBo> newBos = pathDao.getMenu(dtoo);
                bos.get(i).setChildren(newBos);
            }
        }
        return bos;
    }

    @Override
    public List<MenuPathBo> getMenuPath(String userId, int type) {
        List<Role> roles = roleService.getRoleByUserId(userId);
        List<MenuPathBo> zzBo = new ArrayList<>();
        for (Role role : roles) {
            MenuPathQueryDto dto = new MenuPathQueryDto();
            dto.setLevel(1);
            dto.setRoleId(role.getId());
            dto.setType(type);
            zzBo.addAll(pathDao.getMenuPath(dto));
        }
        List<MenuPathBo> zBo = new ArrayList<>();
        for (MenuPathBo menuPathBo : zzBo) {
            Boolean has = false;
            for (MenuPathBo pathBo : zBo) {
                if(pathBo.getMenu_id() == menuPathBo.getMenu_id()){
                    has = true;
                    break;
                }
            }
            if(!has){
                zBo.add(menuPathBo);
            }
        }
        if (zBo!=null){

            for (Role role : roles) {
                for (int i = 0; i < zBo.size(); i++) {
                    MenuPathQueryDto nDto = new MenuPathQueryDto();
                    nDto.setLevel(2);
                    nDto.setRoleId(role.getId());
                    nDto.setParentId(zBo.get(i).getMenu_id());
                    nDto.setType(type);
                    List<MenuPathBo> nBos = pathDao.getMenuPath(nDto);
                    for (MenuPathBo nBo : nBos) {
                        Boolean has = false;
                        if(zBo.get(i).getChildren()!=null){
                            for (MenuPathBo child : zBo.get(i).getChildren()) {
                                if(child.getMenu_id() == nBo.getMenu_id()){
                                    has = true;
                                    break;
                                }
                            }
                        }else {
                            zBo.get(i).setChildren(new ArrayList<>());
                        }
                        if(!has){
                            zBo.get(i).getChildren().add(nBo);
                        }
                    }
                }
            }
        }
        return zBo;
    }
}
